Skip to content

Method: static {...}

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.oom;
19:
20: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
21: import cz.cvut.kbss.jopa.model.metamodel.AbstractPluralAttribute;
22: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
23: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
24: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26: import cz.cvut.kbss.ontodriver.model.Value;
27:
28: import java.net.URI;
29: import java.util.Collection;
30: import java.util.HashSet;
31: import java.util.Objects;
32: import java.util.Set;
33:
34: class SimpleSetPropertyStrategy<X> extends PluralObjectPropertyStrategy<AbstractPluralAttribute<? super X, ?, ?>, X> {
35:
36: SimpleSetPropertyStrategy(EntityType<X> et, AbstractPluralAttribute<? super X, ?, ?> att, Descriptor descriptor,
37: EntityMappingHelper mapper) {
38: super(et, att, descriptor, mapper);
39: }
40:
41: @Override
42: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
43: final Object value = extractFieldValueFromInstance(instance);
44: assert value instanceof Collection || value == null;
45: final Collection<?> valueCollection = (Collection<?>) value;
46: extractValues(valueCollection, valueBuilder);
47: }
48:
49: private <T> void extractValues(Collection<T> valueCollection, AxiomValueGatherer valueBuilder) {
50: if (valueCollection == null) {
51: valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeWriteContext());
52: return;
53: }
54: final Class<?> elemType = attribute.getBindableJavaType();
55: final Set<Value<?>> assertionValues = new HashSet<>(valueCollection.size());
56: if (IdentifierTransformer.isValidIdentifierType(elemType)) {
57: valueCollection.stream().filter(Objects::nonNull).forEach(item -> assertionValues
58: .add(new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(item)))));
59: } else if (elemType.isEnum()) {
60: assert attribute.getConverter() != null;
61: valueCollection.stream().filter(Objects::nonNull).forEach(
62: item -> assertionValues.add(new Value<>(attribute.getConverter().convertToAxiomValue(item))));
63: } else {
64: final EntityType<T> et = (EntityType<T>) mapper.getEntityType(elemType);
65: for (T val : valueCollection) {
66: if (val == null) {
67: continue;
68: }
69: if (referenceSavingResolver.shouldSaveReferenceToItem(val, getAttributeValueContexts())) {
70: final URI valId = EntityPropertiesUtils.getIdentifier(val, et);
71: assert valId != null;
72: assertionValues.add(new Value<>(NamedResource.create(valId)));
73: } else {
74: referenceSavingResolver
75: .registerPendingReference(valueBuilder.getSubjectIdentifier(), createAssertion(), val,
76: getAttributeWriteContext());
77: }
78: }
79: }
80: valueBuilder.addValues(createAssertion(),
81: filterOutInferredValues(valueBuilder.getSubjectIdentifier(), assertionValues),
82: getAttributeWriteContext());
83: }
84: }